<?php include "include/function_library.php"; ?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php include "html/header.html"; ?>

</head>
<body class="tcuk home">

	<div id="wp">
	
		<?php  include "html/menu.html"; ?>
		
		<!-- CONTENT STARTS-->
		<div id="ct"> 	
			
			<?php 
			if(post("tour_submit")) {
				$tour_location 			= post("tour_location");
				$tour_name 				= post("tour_name");
				$tour_description 		= post("tour_description");
				$tour_start_date 		= post("tour_start_date");
				$tour_end_date 			= post("tour_end_date");
				$tour_type 				= post("tour_type");
				$tour_id				= post("id");
								
				$organizer_company_id = "1";
				
				$delete_hotels_query = "delete from hotel_of_tour where tour_id = '$tour_id'";
				$result = sql_query($delete_hotels_query);
				
				$query = "update tour set location='$tour_location', name='$tour_name', description='$tour_description', start_date='$tour_start_date', end_date='$tour_end_date', 
															type='$tour_type' where id = $tour_id";
				
				$link = connect();
				$result = mysql_query($query, $link) or die("Sql Error! " . mysql_error());
				
				
				
				foreach($_POST["hotels_of_tour"] as $hotel) {
					$query = "INSERT INTO hotel_of_tour (tour_id, hotel_id)
									VALUES ('$tour_id', '$hotel')";
					$result = $result && (mysql_query($query, $link) or die("Sql Error! " . mysql_error()));
				}
				mysql_close($link);
				
				if($result) {
					redirect("tour_detail.php?edited=1&id=$tour_id");
				} else {
					echo "<div class='alert error'>Error! Could Not Update! There is(are) error(s) in the form!</div>";
				}
			} 
			
			$id = get("id");
			$query = "SELECT t.id, t.name, location, description, start_date, end_date, type, oc.id AS oc_id, oc.name AS oc_name FROM tour t, organizer_company oc 
												WHERE t.organizer_company_id=oc.id AND t.id=$id";
				
			$result = sql_query($query);
			$tuple = mysql_fetch_assoc($result);
			
			
			$queryHotels = "select h.id, h.name, ht.tour_id from hotel h left outer join hotel_of_tour ht on h.id=ht.hotel_id and ht.tour_id = '$id'";
			$resultOfHotels = sql_query($queryHotels);
			?>
			
			<form action="<?php echo curPageURL(); ?>?id=<?php echo $id ?>" method="post">
			<ul id="signup_form" style="width: 50%; margin-left: auto; margin-right: auto; margin-top: 30px; list-style: none;">
				<li style="border: none;">
					<label>Name </label>
					<input type="text" id="tour_name" name="tour_name" value="<?php echo $tuple["name"]; ?>" placeholder="Tour name.." style="width: 250px;"/>
				</li>
				<li>
					<label>Location </label>
					<input type="text" id="tour_location" name="tour_location" value="<?php echo $tuple["location"]; ?>" placeholder="Location.." style="width: 250px;"/>
				</li>
				<li>
					<label>Description </label>
					<textarea rows="10" id="tour_description" name="tour_description" placeholder="Description.." style="width: 250px;"><?php echo $tuple["description"]; ?></textarea>
				</li>
				<li>
					<label>Start Date</label>
					<input type="date" id="tour_start_date" name="tour_start_date" value="<?php echo $tuple["start_date"]; ?>" placeholder="Start date.." style="width: 120px;"/>
				</li>
				<li>
					<label>End Date </label>
					<input type="date" id="tour_end_date" name="tour_end_date" value="<?php echo $tuple["end_date"]; ?>" placeholder="End date.." style="width: 120px;"/>
				</li>
				<li>
					<label>Type </label>
					<select id="tour_type" name="tour_type">
						<option value="domestic" selected="<?php $tuple["name"] == 'domestic' ?>">Domestic</option>
						<option value="abroad" selected="<?php $tuple["name"] == 'abroad' ?>">Abroad</option>
					</select>
				</li>
				<li>
					<label>Hotels(press ctrl for multiple selection) </label>
					
					<select id="hotels_of_tour[]" name="hotels_of_tour[]" multiple>
					<?php 
					while($tuple = mysql_fetch_assoc($resultOfHotels)) {
						if($tuple["tour_id"]==null) {
							?><option value="<?php echo $tuple["id"] ?>"><?php echo $tuple["name"] ?></option><?php 
						} else {
							?><option value="<?php echo $tuple["id"] ?>" selected><?php echo $tuple["name"] ?></option><?php 
						}
					}?>
						
					</select>
				</li>
				<li>
					<input type="hidden" name="id" value="<?php echo $id ?>"/>
					<input type="submit" id="tour_submit" name="tour_submit" value="Save" style="width: 90px; height:30px; margin-left: 80px;"/>
				</li>
			</ul>
			</form>
		
		</div> 	
		<!-- CONTENT ENDS-->		
		
		<?php include "html/footer.html"; ?>
		
	</div>
	
</body>
</html>